/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * 
 * @author ZhangMaoren
 * @since 2015年6月17日
 * @version 0.1
 */
package com.suredy.base.ctrl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.base.model.Role;
import com.suredy.base.model.User;
import com.suredy.base.model.User2Role;
import com.suredy.base.service.RoleSrv;
import com.suredy.base.service.User2RoleSrv;
import com.suredy.base.service.UserSrv;
import com.suredy.core.ctrl.BaseCtrl;

/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value = "config")
public class RoleCtrl extends BaseCtrl {

	private final static Logger log = LoggerFactory.getLogger(RoleCtrl.class);
	@Autowired
	private RoleSrv roleSrv;

	@Autowired
	private User2RoleSrv u2rSrv;

	@Autowired
	private UserSrv userSrv;

	/**
	 * get role list
	 * 
	 * @param type
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "role-list")
	public ModelAndView getRoles() {
		ModelAndView view = new ModelAndView("config/role-list");
		List<Role> data = this.roleSrv.getAll();
		view.addObject("data", data);
		return view;
	}

	@RequestMapping(value = "role2user")
	public ModelAndView Role2User(String roleId) {
		ModelAndView view = new ModelAndView("config/role2user");
		Role role = this.roleSrv.getById(roleId);
		List<User> users = this.userSrv.getAll();
		List<User2Role> relations = this.u2rSrv.getByRole(roleId);
		view.addObject("role", role);
		view.addObject("relations", relations);
		view.addObject("users", users);
		return view;
	}

	@Transactional
	@RequestMapping("role2user-save")
	@ResponseBody
	public String Role2ResourceSave(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			log.error("Parameter String[id] is blank.");
			return "0";
		}
		// 删除全部user2role引用
		List<User2Role> relations = this.u2rSrv.getByRole(id);
		for (User2Role ur : relations) {
			this.u2rSrv.delete(ur);
		}
		// 加入新的user2role引用
		String[] userIds = request.getParameterValues("userId");
		if (userIds != null && userIds.length > 0) {
			for (String userId : userIds) {
				User2Role ur = new User2Role();
				ur.setRoleId(id);
				ur.setUserId(userId);
				this.u2rSrv.save(ur);
			}
		}
		return "1";
	}
	
	@RequestMapping(value = "user2role")
	public ModelAndView User2Role(String userId) {
		ModelAndView view = new ModelAndView("config/user2role");
		User user = this.userSrv.getById(userId);
		List<Role> roles = this.roleSrv.getAll();
		
		List<User2Role> relations = this.u2rSrv.getByUser(userId);
		view.addObject("user", user);
		view.addObject("relations", relations);
		view.addObject("roles", roles);
		return view;
	}

	@Transactional
	@RequestMapping("user2role-save")
	@ResponseBody
	public String User2RoleSave(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			log.error("Parameter String[id] is blank.");
			return "0";
		}
		// 删除全部user2role引用
		List<User2Role> relations = this.u2rSrv.getByUser(id);
		for (User2Role ur : relations) {
			this.u2rSrv.delete(ur);
		}
		// 加入新的user2role引用
		String[] roleIds = request.getParameterValues("roleId");
		if (roleIds != null && roleIds.length > 0) {
			for (String roleId : roleIds) {
				User2Role ur = new User2Role();
				ur.setRoleId(roleId);
				ur.setUserId(id);
				this.u2rSrv.save(ur);
			}
		}
		return "1";
	}
}
